//
//  LOLRuneViewController.m
//  LOLXXbox
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLRuneViewController.h"
#import "LOLRuneTableViewCell.h"
#import "LSZNetWorkTools.h"
#import "LOLRuneModel.h"
#import <UIImageView+WebCache.h>
@interface LOLRuneViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
//级别
@property (nonatomic, strong) UITableView *levelTableView;
//类型
@property (nonatomic, strong) UITableView *typeTableView;
//符文类型
@property (nonatomic, strong) NSArray *typeArray;
//几级
@property (nonatomic, assign) NSInteger level;
//级别button
@property (nonatomic, strong) UIButton *levelButton;
//类型button
@property (nonatomic, strong) UIButton *typeButton;
//红色印记
@property (nonatomic, strong) NSMutableArray *redArray;
//蓝色雕文
@property (nonatomic, strong) NSMutableArray *blueArray;
//黄色符印
@property (nonatomic, strong) NSMutableArray *yellowArray;
//紫色精华
@property (nonatomic, strong) NSMutableArray *purpleArray;

@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation LOLRuneViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"符文列表";
    self.typeArray = @[@"印记", @"雕文", @"符印", @"精华"];
    [self creatTableView];
    [self creatTwoTableView];
    [self getData];
    self.level = 3;
    
    self.redArray = [NSMutableArray array];
    self.blueArray = [NSMutableArray array];
    self.yellowArray = [NSMutableArray array];
    self.purpleArray = [NSMutableArray array];
    self.dataSource = [NSMutableArray array];
}

- (void)creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 150;
    [self.tableView registerClass:[LOLRuneTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    
}

- (void)creatTwoTableView{
    self.levelTableView = [[UITableView alloc] initWithFrame:CGRectMake(40, 30, self.view.frame.size.width / 2 - 80, 90) style:UITableViewStylePlain];
    self.levelTableView.dataSource = self;
    self.levelTableView.delegate = self;
    [self.levelTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
    self.levelTableView.rowHeight = 30;
    [self.view addSubview:self.levelTableView];
    
    self.typeTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 40, 30, self.view.frame.size.width / 2 - 80, 120) style:UITableViewStylePlain];
    self.typeTableView.dataSource = self;
    self.typeTableView.delegate = self;
    [self.typeTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.typeTableView.rowHeight = 30;
    [self.view addSubview:self.typeTableView];
    
    self.levelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.levelButton.frame = CGRectMake(40, 0, self.view.frame.size.width / 2 - 80, 30);
    [self.levelButton setTitle:@"3级" forState:UIControlStateNormal];
    [self.levelButton addTarget:self action:@selector(levelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.levelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.view addSubview:self.levelButton];
    
    self.typeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.typeButton.frame = CGRectMake(self.view.frame.size.width / 2 + 40, 0, self.view.frame.size.width / 2 - 80, 30);
    [self.typeButton setTitle:@"印记" forState:UIControlStateNormal];
    [self.typeButton addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.typeButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.view addSubview:self.typeButton];
    
    self.levelTableView.hidden = YES;
    self.typeTableView.hidden = YES;
}

- (void)getData{
    [LSZNetWorkTools getWithUrl:@"http://lolbox.duowan.com/phone/apiRunes.php" parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSArray *array = [result objectForKey:@"Red"];
        for (NSDictionary *dic in array) {
            LOLRuneModel *model = [[LOLRuneModel alloc] initWithDataSource:dic];
            [self.redArray addObject:model];
        }
        NSArray *array2 = [result objectForKey:@"Blue"];
        for (NSDictionary *dic in array2) {
            LOLRuneModel *model = [[LOLRuneModel alloc] initWithDataSource:dic];
            [self.blueArray addObject:model];
        }
        
        NSArray *array3 = [result objectForKey:@"Yellow"];
        for (NSDictionary *dic in array3) {
            LOLRuneModel *model = [[LOLRuneModel alloc] initWithDataSource:dic];
            [self.yellowArray addObject:model];
        }
        NSArray *array4 = [result objectForKey:@"Purple"];
        for (NSDictionary *dic in array4) {
            LOLRuneModel *model = [[LOLRuneModel alloc] initWithDataSource:dic];
            [self.purpleArray addObject:model];
        }
        
        self.dataSource = self.redArray;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } fail:^(NSError *error) {
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.levelTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor orangeColor];
        cell.textLabel.text = [NSString stringWithFormat:@"%ld级",indexPath.row + 1];
        return cell;
    }else if (tableView == self.typeTableView){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.backgroundColor = [UIColor orangeColor];
        cell.textLabel.text = self.typeArray[indexPath.row];
        return cell;
    }else{
        LOLRuneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.runeModel = self.dataSource[indexPath.row];
//        cell.level = self.level;
        
        [cell.imageViews sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/runes/%@_%ld.png",cell.runeModel.Img,self.level]]];
    
        switch (self.level) {
            case 1:
                cell.priceLabel.text = [NSString stringWithFormat:@"%@级 游戏币 %@",@1,cell.runeModel.iplev1];
                cell.levLabel.text = [NSString stringWithFormat:@"%@%@",cell.runeModel.lev1,cell.runeModel.Prop];
                
                break;
            case 2:
                cell.priceLabel.text = [NSString stringWithFormat:@"%@级 游戏币 %@",@2,cell.runeModel.iplev2];
                cell.levLabel.text = [NSString stringWithFormat:@"%@%@",cell.runeModel.lev2,cell.runeModel.Prop];
                
                break;
                
            case 3:
                cell.priceLabel.text = [NSString stringWithFormat:@"%@级 游戏币 %@",@3,cell.runeModel.iplev3];
                cell.levLabel.text = [NSString stringWithFormat:@"%@%@",cell.runeModel.lev3,cell.runeModel.Prop];
                
                break;
                
                
            default:
                break;
        }
        return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.levelTableView) {
        return 3;
    }else if (tableView == self.typeTableView){
        return 4;
    }else{
        return self.dataSource.count;
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.levelTableView) {
        
        [self.levelButton setTitle:[NSString stringWithFormat:@"%ld级",indexPath.row+1] forState:UIControlStateNormal];
        tableView.hidden = YES;
        
        self.level = indexPath.row + 1;
        [self.tableView reloadData];
        
    }
    else if(tableView == self.typeTableView)
    {
        
        
        [self.typeButton setTitle:self.typeArray[indexPath.row] forState:UIControlStateNormal];
        switch (indexPath.row) {
            case 0:
                if (self.dataSource != self.redArray)
                {
                    self.dataSource = self.redArray;
                    [self.tableView reloadData];
                }
                break;
            case 1:
                if (self.dataSource != self.blueArray)
                {
                    self.dataSource = self.blueArray;
                    [self.tableView reloadData];
                }
                break;
                
            case 2:
                if (self.dataSource != self.yellowArray)
                {
                    self.dataSource = self.yellowArray;
                    [self.tableView reloadData];
                }
                break;
                
            case 3:
                if (self.dataSource != self.purpleArray)
                {
                    self.dataSource = self.purpleArray;
                    [self.tableView reloadData];
                }
                break;
                
                
            default:
                break;
        }
        tableView.hidden = YES;
    }
    
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (void)levelButtonAction:(UIButton *)sender{
    self.levelTableView.hidden = NO;
    self.typeTableView.hidden = YES;
}

- (void)typeButtonAction:(UIButton *)sender{
    self.levelTableView.hidden = YES;
    self.typeTableView.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
